<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="内容分类" prop="type"> -->
        <!-- <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <!-- <el-select v-model="queryParams.type" placeholder="请选择内容分类" > -->
            <!-- <el-option v-for="item in tvTypeOptions" :key="item.id" :label="item.name" :value="item.id" :disabled="item.status == 1" ></el-option> -->
            <!-- </el-select> -->
      <!-- </el-form-item> -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
          v-hasPermi="['monitor:synchData:remove']"
        >清空</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="synchDataList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="编号" align="center" prop="id"   width="80" />
      <!-- <el-table-column label="内容分类" align="center" prop="type"  width="120" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_video_type" :value="scope.row.type"/>
        </template> 
      </el-table-column> -->
      <el-table-column label="内容同步状态" align="center" prop="status"  width="130" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_synch_data_status" :value="scope.row.status"/>
        </template> 
      </el-table-column>
      <!-- <el-table-column label="版本号" align="center" prop="version" /> -->
      <el-table-column label="内容同步文件" align="center" prop="url" />
      <el-table-column label="待同步内容" align="center" prop="cloudPath" />
      <el-table-column label="内容同步目录" align="center" prop="unzipPath" />
      <!-- <el-table-column label="内容备注" align="center" prop="remake" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="更新时间" align="center" prop="updateTime" width="150">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.updateTime) }}</span>
                </template>
              </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:synchData:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改内容同步记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="已同步的压缩包地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入已同步的压缩包地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="对应云服务器上待同步的压缩包地址" prop="cloudPath">
          <el-input v-model="form.cloudPath" placeholder="请输入对应云服务器上待同步的压缩包地址" />
        </el-form-item>
        <el-form-item label="服务器上已解压缩目录" prop="unzipPath">
          <el-input v-model="form.unzipPath" placeholder="请输入服务器上已解压缩目录" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSynchData, getSynchData, delSynchData, cleanSynchData,addSynchData, updateSynchData } from "@/api/system/synchData";
import { getContentType,getContentTypeData } from '@/api/index';

export default {
  name: "SynchData",
  dicts: ['sys_video_type','sys_synch_data_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 内容分类选项
      tvTypeOptions: [],
      // 内容同步记录表格数据
      synchDataList: [],
       // 日期范围
       dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        version: null,
        url: null,
        cloudPath: null,
        unzipPath: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // this.getContentType();
    this.getList();
  },
  methods: {
    getContentType(){
      getContentType().then(response =>{
        this.tvTypeOptions = response.data;
        if(this.queryParams.type !== null){
          this.getContentTypeData({
          "type": this.queryParams.type,
         });
        }
     
      }).catch(error => {
        console.log("获取内容分类的响应:"+error);
        return
      });

    },
    handleClean() {
      this.$modal.confirm('是否确认清空所有内容同步记录数据项？').then(function() {
        return cleanSynchData();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("清空成功");
      }).catch(() => {});
    },
    /** 查询内容同步记录列表 */
    getList() {
      this.loading = true;
      // listSynchData(this.queryParams).then(response => {
      listSynchData(this.addDateRange(this.queryParams, this.dateRange)).then(response => {        
        this.synchDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        version: null,
        createTime: null,
        url: null,
        remark: null,
        cloudPath: null,
        unzipPath: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加内容同步记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSynchData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改内容同步记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSynchData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSynchData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除内容同步记录编号为"' + ids + '"的数据项？').then(function() {
        return delSynchData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/synchData/export', {
        ...this.queryParams
      }, `synchData_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
